﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using EShop.Model;
using EShop.Common;

namespace EShop.DAL
{
    public class Merchandise_BasicInfoDAL:BaseDAL
    {

        /// <summary>
        /// 取出所有商品信息数据
        /// </summary>
        /// <returns></returns>
        public List<MerchandiseBasicInfo> GetAllMerchandiseBasicInfo()
        {
            string strUSP = "USP_Merchandise_BasicInfo_GetAllList";
            List<MerchandiseBasicInfo> aMerchandiseBasicInfoList=new List<MerchandiseBasicInfo>();
            try
            {
                SqlDataReader reader = (SqlDataReader)DbHelper.ExecuteReaderSQL(strUSP, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    MerchandiseBasicInfo oInfo=new MerchandiseBasicInfo();
                    oInfo.Barcode=reader["Barcode"].ToString();
                    oInfo.MerchandiseName = reader["MerchandiseName"].ToString();
                    oInfo.MerchandiseClassID=reader.GetInt32(reader.GetOrdinal("MerchandiseClassID"));
                    oInfo.MerchandiseClassName = reader["MerchandiseClassName"].ToString();
                    oInfo.MerchandiseUnitID = reader.GetInt32(reader.GetOrdinal("MerchandiseUnitID"));
                    oInfo.MerchandiseUnitName = reader["UnitName"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("MerchandiseResume")))
                        oInfo.MerchandiseResume = reader["MerchandiseResume"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("MerchandiseRemark")))
                        oInfo.MerchandiseRemark = reader["MerchandiseRemark"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("IsAlert")))
                        oInfo.IsAlert = reader.GetBoolean(reader.GetOrdinal("IsAlert"));
                    if (!reader.IsDBNull(reader.GetOrdinal("AlertNum")))
                        oInfo.AlertNum = reader.GetInt32(reader.GetOrdinal("AlertNum"));
                    if (!reader.IsDBNull(reader.GetOrdinal("HasOverTime")))
                        oInfo.HasOverTime = reader.GetBoolean(reader.GetOrdinal("HasOverTime"));

                    aMerchandiseBasicInfoList.Add(oInfo);
                }
                reader.Close();

                return aMerchandiseBasicInfoList;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 查找相应的商品数据
        /// </summary>
        /// <param name="strSearchValue">商品名称或是商品条码</param>
        /// <param name="iMerchandiseClassID">商品分类</param>
        /// <returns></returns>
        public List<MerchandiseBasicInfo> SearchMerchandiseBasicInfo(string strSearchValue, int iMerchandiseClassID)
        {
            string strUSP = "USP_Search_MerchandiseBasicInfo";
            List<MerchandiseBasicInfo> aMerchandiseBasicInfoList = new List<MerchandiseBasicInfo>();

            try
            {
                SqlParameter[] parameters = {
                                                new SqlParameter("@SearchValue",strSearchValue), 
                                                new SqlParameter("@MerchandiseClassID",iMerchandiseClassID)
                                            };

                SqlDataReader reader = (SqlDataReader)DbHelper.ExecuteReaderSQL(strUSP, CommandType.StoredProcedure, parameters);
                while (reader.Read())
                {
                    MerchandiseBasicInfo oInfo = new MerchandiseBasicInfo();
                    oInfo.Barcode = reader["Barcode"].ToString();
                    oInfo.MerchandiseName = reader["MerchandiseName"].ToString();
                    oInfo.MerchandiseClassID = reader.GetInt32(reader.GetOrdinal("MerchandiseClassID"));
                    oInfo.MerchandiseClassName = reader["MerchandiseClassName"].ToString();
                    oInfo.MerchandiseUnitID = reader.GetInt32(reader.GetOrdinal("MerchandiseUnitID"));
                    oInfo.MerchandiseUnitName = reader["UnitName"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("MerchandiseResume")))
                        oInfo.MerchandiseResume = reader["MerchandiseResume"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("MerchandiseRemark")))
                        oInfo.MerchandiseRemark = reader["MerchandiseRemark"].ToString();
                    if (!reader.IsDBNull(reader.GetOrdinal("IsAlert")))
                        oInfo.IsAlert = reader.GetBoolean(reader.GetOrdinal("IsAlert"));
                    if (!reader.IsDBNull(reader.GetOrdinal("AlertNum")))
                        oInfo.AlertNum = reader.GetInt32(reader.GetOrdinal("AlertNum"));
                    if (!reader.IsDBNull(reader.GetOrdinal("HasOverTime")))
                        oInfo.HasOverTime = reader.GetBoolean(reader.GetOrdinal("HasOverTime"));

                    aMerchandiseBasicInfoList.Add(oInfo);
                }
                reader.Close();

                return aMerchandiseBasicInfoList;

            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("USP: {0}\r\n strSearchValue:{1}\r\n iMerchandiseClassID: {2} ex:{3}", strUSP,strSearchValue,iMerchandiseClassID, ex.Message));
            }
        }


        /// <summary>
        /// 添加一条商品信息数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(MerchandiseBasicInfo model)
        {
            string strUSP = "USP_Add_MerchandiseBasicInfo";
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@Barcode", SqlDbType.VarChar,30),
					new SqlParameter("@MerchandiseClassID", SqlDbType.Int),
					new SqlParameter("@MerchandiseName", SqlDbType.VarChar,50),
					new SqlParameter("@MerchandiseUnitID", SqlDbType.Int),
					new SqlParameter("@MerchandiseResume", SqlDbType.Text),
					new SqlParameter("@MerchandiseRemark", SqlDbType.Text),
					new SqlParameter("@IsAlert", SqlDbType.Bit,1),
					new SqlParameter("@AlterNum", SqlDbType.Int),
					new SqlParameter("@HasOverTime", SqlDbType.Bit,1)};
                parameters[0].Value = model.Barcode;
                parameters[1].Value = model.MerchandiseClassID;
                parameters[2].Value = model.MerchandiseName;
                parameters[3].Value = model.MerchandiseUnitID;
                parameters[4].Value = model.MerchandiseResume;
                parameters[5].Value = model.MerchandiseRemark;
                parameters[6].Value = model.IsAlert;
                parameters[7].Value = model.AlertNum;
                parameters[8].Value = model.HasOverTime;

                object obj = DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters);
                if ((int)obj > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("USP:{0},\r\n Model info :\r\n {1}\r\n ex:{2}", strUSP, model.ToString(), ex.Message));
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(string strBarCode, MerchandiseBasicInfo model)
        {
            string strUSP = "USP_Merchandise_BasicInfo_Update";

            try
            {

                SqlParameter[] parameters = {
                                                new SqlParameter("@Barcode", SqlDbType.VarChar, 30),
                                                new SqlParameter("@MerchandiseClassID", SqlDbType.Int),
                                                new SqlParameter("@MerchandiseName", SqlDbType.VarChar, 50),
                                                new SqlParameter("@MerchandiseUnitID", SqlDbType.Int),
                                                new SqlParameter("@MerchandiseResume", SqlDbType.Text),
                                                new SqlParameter("@MerchandiseRemark", SqlDbType.Text),
                                                new SqlParameter("@IsAlert", SqlDbType.Bit, 1),
                                                new SqlParameter("@AlertNum", SqlDbType.Int, 4),
                                                new SqlParameter("@HasOverTime", SqlDbType.Bit, 1),
                                                new SqlParameter("@OldBarCode", SqlDbType.VarChar, 30),
                                            };
                parameters[0].Value = model.Barcode;
                parameters[1].Value = model.MerchandiseClassID;
                parameters[2].Value = model.MerchandiseName;
                parameters[3].Value = model.MerchandiseUnitID;
                parameters[4].Value = model.MerchandiseResume;
                parameters[5].Value = model.MerchandiseRemark;
                parameters[6].Value = model.IsAlert;
                parameters[7].Value = model.AlertNum;
                parameters[8].Value = model.HasOverTime;
                parameters[9].Value = strBarCode;

                int rows = DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters);
                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {                
                throw new Exception(string.Format("USP:{0},\r\n Model:{1},\r\n ex:{2}",strUSP,model.ToString(),ex.Message));
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Barcode)
        {

            string strUSP = "USP_Merchandise_BasicInfo_Delete";
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@Barcode", SqlDbType.VarChar,50)};
                parameters[0].Value = Barcode;

                int rows = DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters);
                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
    }
}
